package tt.dz.appserver.service.business.imp;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.ChargerReserveDao;
import tt.dz.appserver.dao.ext.StationCommentDao;
import tt.dz.appserver.entity.vo.ReservationDetailVo;
import tt.dz.appserver.entity.vo.charger.ChargerDetailVo;
import tt.dz.appserver.entity.vo.charger.DeviceTypeInfoVo;
import tt.dz.appserver.entity.vo.station.StationDetailVo;
import tt.dz.appserver.service.avro.client.oms.dzttOmsServiceProtocol;
import tt.dz.appserver.service.business.ReservationService;
import tt.dz.appserver.util.CodeUtils;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.dzcode.OmsCode;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.json.JsonUtils;


/**
 * 预约相关业务接口实现类
 * @author Administrator
 *
 */
public class ReservationServiceImpl implements ReservationService{

	/**日志*/
	private static final Logger LOG = Logger.getLogger("[预约接口]:");
	
	/**OMS数据访问接口*/
	@Resource(name="omsMessageProtocolRequestor")
	private dzttOmsServiceProtocol omsService;
	
	/**预约订单数据操作dao*/
	@Resource
	private ChargerReserveDao chargerReserveDao;
	
	@Resource
	private StationCommentDao stationCommentDao;
	
	
	@Override
	public JSONObject reservation(String userCard, String chargerId, String plugNo) throws AvroRemoteException{
		
		
		JSONObject resultJSONObject = new JSONObject();
		
		//调用预约接口
		Object resultJson = omsService.chargeReservation(userCard, chargerId, plugNo);
		
		LOG.info("用户卡号为【"+userCard+"】预约 【"+chargerId+"】桩 【"+plugNo+"】端口,OMS返回结果:"+resultJson);
		String resultJsonString = resultJson.toString();
		
		//JSON转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		//获取返回代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
		if(Response.SUCCESS.getCode().equals(code)){

			resultJSONObject.put("code", Response.SUCCESS.getCode());
			resultJSONObject.put("msg", Response.SUCCESS.getMsg());
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJSONObject.put("code", response.getCode());
			resultJSONObject.put("msg", response.getMsg());
		}
		
		return resultJSONObject;
	}
	
	@Override
	public JSONObject cancelReservation(String reservationSn) throws AvroRemoteException{
		
		JSONObject resultJSONObject = new JSONObject();
		
		//调用取消预约接口
		String resultJsonString = omsService.cancelReservation(reservationSn).toString();

		LOG.info("OMS取消 【"+reservationSn+"】 此预约流水号,返回结果:"+resultJsonString);
		
		//JSON转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		//获取返回代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
		if(Response.SUCCESS.getCode().equals(code)){

			resultJSONObject.put("code", Response.SUCCESS.getCode());
			resultJSONObject.put("msg", Response.SUCCESS.getMsg());
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJSONObject.put("code", response.getCode());
			resultJSONObject.put("msg", response.getMsg());
		}
		
		return resultJSONObject;
	}

	@Override
	public JSONObject reservationList(String userCard, int pageNo) throws AvroRemoteException{
		
		JSONObject resultJSONObject = new JSONObject();
		
		//调用预约列表接口
		String resultJsonString = omsService.getReservationListByPage(userCard, pageNo).toString();

		LOG.info("用户卡号为【"+userCard+"】获取预约列表,OMS返回结果:"+resultJsonString);
		
		//JSON转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		//获取返回代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
		if(Response.SUCCESS.getCode().equals(code)){
			
			JSONArray reservationList = omsResultObject.getJSONArray("list");

			resultJSONObject.put("reservationList", reservationList);
			resultJSONObject.put("code", Response.SUCCESS.getCode());
			resultJSONObject.put("msg", Response.SUCCESS.getMsg());
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJSONObject.put("code", response.getCode());
			resultJSONObject.put("msg", response.getMsg());
		}
		
		return resultJSONObject;
	}

	@Override
	public JSONObject getReservationDetail(String reservationSn) throws AvroRemoteException{
		
		JSONObject resultJSONObject = null;
		
		//调用预约记录详细接口
		String resultJsonString = omsService.getReservationDetailById(reservationSn).toString();

		LOG.info("通过此【"+reservationSn+"】流水号获取预约详细, OMS预约详细返回结果:"+resultJsonString);
		
		//JSON转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		//获取返回代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
		if(Response.SUCCESS.getCode().equals(code)){
			
			resultJSONObject = JsonUtils.getSuccessJson();
			JSONObject reservation = omsResultObject.getJSONObject("pojo");
			ReservationDetailVo reservationDetail = JsonUtils.JsonToObjectOfClass(reservation, ReservationDetailVo.class);
			
			
			/**=========================================获取电站详细 begin====================================================*/
			String stationId = reservationDetail.getStationId();
			omsService.getStationDetailByStationId(stationId);
			String stationJsonString = omsService.getStationDetailByStationId(stationId).toString();
			// json转换
			JSONObject stationResultObject = JSONObject.parseObject(stationJsonString);

			String stationCode = stationResultObject.getString(Constants.OmsServerResultField.CODE);

			LOG.info("获取 【" + stationId + "】 站点详细,OMS返回代码为:" + stationCode);
			if (OmsCode.SUCCESS.equals(stationCode)) {
				// 获取站点详细
				JSONObject stationObject = stationResultObject.getJSONObject("pojo");
				StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(stationObject, StationDetailVo.class);
				String grade = stationCommentDao.findStationCommentGrade(stationId);
				stationDetailVo.setGrade(String.valueOf(grade));
				
				//封装站点详细
				reservationDetail.setStationDetail(stationDetailVo);
			}
			/**=========================================获取电站详细 End====================================================*/
			

			/**=========================================获取桩详细 begin====================================================*/
			String chargerId = reservationDetail.getChargerId();
			
			String chargerJsonString = omsService.getChargerDetailByChargerId(chargerId).toString();
			// json转换
			JSONObject chargerResultObject = JSONObject.parseObject(chargerJsonString);
			// 获取操作结果
			String chargerCode = chargerResultObject.getString(Constants.OmsServerResultField.CODE);
			LOG.info("获取电桩ID为【" + chargerId + "】 电桩详细接口,OMS返回代码为:" + chargerCode);

			// 操作成功
			if (OmsCode.SUCCESS.equals(chargerCode)) {
				JSONObject charger = chargerResultObject.getJSONObject("pojo");
				// 封装字段
				ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
				//设备类型
				JSONObject deviceTypeJson = charger.getJSONObject("deviceType");
				if(deviceTypeJson!=null){
					DeviceTypeInfoVo deviceTypeVo = JsonUtils.JsonToObjectOfClass(deviceTypeJson, DeviceTypeInfoVo.class);
					chargerDetailVo.setDeviceType(deviceTypeVo);
				}
				//封装电桩详细
				reservationDetail.setChargerDetail(chargerDetailVo);
				
			}
			/**=========================================获取桩详细 End====================================================*/
			
			resultJSONObject.put("reservationDetail", reservationDetail);
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJSONObject = JsonUtils.getFailJson(response);
		}
		
		return resultJSONObject;
		
	}
}
